package com;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.lang.ClassNotFoundException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.ResultSet;

//�������е���������ݿ�����Э��Ӧ��ʹ�������ļ��������ô���

//���ڸ�����ݿ�����Ӷ����Ҫ��ר�ŵĺ���ȥ�ر���

//����ֱ�ӻ������Ҫ��ÿһ������

public class DbSQL{
   private static String driver;
   private static String url;
   private static String user;
   private static String password;
   private static Connection con;
   private static Statement stm;
   private static PreparedStatement preparestm;
   static{
      try{
       BufferedReader buf = new BufferedReader(new FileReader("D:\\xiaonei1227\\WebContent\\sql.txt"));
       driver = (String)buf.readLine();
       url = (String)buf.readLine();
       user = (String)buf.readLine();
       password = (String)buf.readLine();
       Class.forName(driver).newInstance();//ע�������
       System.out.println("�ɹ���װ��!!");
      }catch(ClassNotFoundException e){
      	 System.out.println(e.toString());
      }catch(InstantiationException e){
      	 System.out.println(e.toString());
      }catch(IllegalAccessException e){
      	 System.out.println(e.toString());
      }catch(FileNotFoundException e){
         e.printStackTrace();	
      }catch(IOException e){
         e.printStackTrace();
      }
      //�����������г�ʼ������ı���
      
   }//��ֻ֤���һ������
   
   public DbSQL(){
      
   }
   public static Connection getCon(){
   	  try{
   	  con = DriverManager.getConnection(url,user,password);
      return con;
      }catch(SQLException e){
      	 System.out.println(e.toString());
      }
      return null;
   }
   public static void closeCon(){
   	  if(con!=null)
   	  try{
   	  	con.close();
   	  }catch(SQLException e){
      	 System.out.println(e.toString());
      }
   }
   public static Statement getstm(){
   	  getCon();
   	  if(con!=null)
      try{
      stm = con.createStatement();
      return stm;
      }catch(SQLException e){
      	 System.out.println(e.toString());
      }
      return null;
   }

   public static void closestm(){
      if(stm!=null)
      try{
      stm.close();
      }catch(SQLException e){
      	 System.out.println(e.toString());
      }
   }
   
   public static PreparedStatement getpreparestm(String sql){
      getCon();
   	  if(con!=null)
      try{
      preparestm = con.prepareStatement(sql);
      return preparestm;
      }catch(SQLException e){
      	 System.out.println(e.toString());
      }
      return null; 	
   }
   public static void closepreparestm(){
      if(preparestm!=null)
      try{
      preparestm.close();
      }catch(SQLException e){
      	 System.out.println(e.toString());
      }
   }
   
   public static void main(String[] agrs){
   	    Statement stm = DbSQL.getstm();
      	DbSQL.closestm();
      	DbSQL.closeCon();
   }
}
